/**
 * Created by cyh on 16/1/31.
 */
$(document).ready(function () {
    String.prototype.endsWith = function (suffix) {
        return this.indexOf(suffix, this.length - suffix.length) !== -1;
    };
    String.prototype.startsWith = function (str) {
        return !this.indexOf(str);
    };


    function getCookie(name) {
        var r = document.cookie.match("\\b" + name + "=([^;]*)\\b");
        return r ? r[1] : undefined;
    }

    $('#submit').click(function () {
        var ico_url = $('#headico').val();
        var domain = $('#domain').val();
        if (!domain.endsWith('/')) domain+='/';
        if (!domain.startsWith('http://')) domain='http://'+domain;
        $('#domain').val(domain);
        if (ico_url == 'head.jpg') {
            ico_url = domain + '%2f' + ico_url+'?v='+Math.random()
        }
        $.post('/conf-submit', {
            title: $('#title').val(),
            signature: $('#signature').val(),
            domain: domain,
            headico: ico_url,
            password: $('#password').val(),
            auth_name: $('#auth_name').val(),
            _xsrf: getCookie("_xsrf")
        }, function (res) {
            if (res.code == 1) {
                UIkit.notify("<i class='uk-icon-check'></i>" + res.msg)
            }
            else {
                UIkit.notify("<i class='uk-icon-close'></i>" + res.msg)
            }
        }, 'json')
    });
    $('#headico').change(function(){
        changeIco();
    });
    $('#ico').error(function(){
        $('.avatar').hide();
        $('#ico_tip').show();
    });
    function changeIco(){
        $('#ico_tip').hide();
        var ico_url=$('#headico').val();
        $('#ico').attr('src',ico_url);
        $('.avatar').show();
    }
    changeIco();

});